package com.quanyan.stadium.biz.redis.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.redisCluster.utils.SerializeUtil;
import com.quanyan.stadium.biz.redis.GameCacheDao;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.component.cache.StadiumRedisConstants;
import com.quanyan.stadium.entity.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author xiaoxiao
 * @date 16/4/11-14:42
 *
 * 缓存基础类
 */

@Service("gameCacheDao")
public class GameCacheDaoImpl implements GameCacheDao {

    private final static Logger logger = LoggerFactory.getLogger(GameCacheDaoImpl.class);

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    private final static int EXPIRE_TIME = 24*60*60-10;

    private final static String DEFAULT_GAME_CONFIG_KEY = "DEFAULT_GAME_CONFIG_KEY_";

    private final static String CLUB_GAME_CONFIG_KEY = "CLUB_GAME_CONFIG_KEY_";

    private final static String BLUCK_USER_KEY = "BLUCK_USER_KEY";


    @Override
    public void initGameEventToCache(GameEvent gameEvent) {
        String key = StadiumRedisConstants.getGameEventKey(gameEvent.getId());
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        RedisKey redisFlagKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, "GAME_EVENT_UPDATE_KEY_"+gameEvent.getId());
        long flag = myRedisTemplate.setnx(redisFlagKey);
        if (flag != 0) {
            myRedisTemplate.setWithExpireTime(redisKey, gameEvent, EXPIRE_TIME);
            myRedisTemplate.delteKey(redisFlagKey);
        }
    }

    @Override
    public void delGameEventFromCache(Integer gameEventId) {
        String key = StadiumRedisConstants.getGameEventKey(gameEventId);
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        myRedisTemplate.delteKey(redisKey);
    }

    @Override
    public void initGameRoundToCache(GameRound gameRound) {
        String key = StadiumRedisConstants.getGameRoundKey(gameRound.getId());
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        RedisKey redisFlagKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, "GAME_ROUND_UPDATE_KEY_"+gameRound.getId());
        long flag = myRedisTemplate.setnx(redisFlagKey);
        if (flag != 0) {
            myRedisTemplate.setWithExpireTime(redisKey, gameRound, EXPIRE_TIME);
            myRedisTemplate.delteKey(redisFlagKey);
        }
    }

    @Override
    public void delGameRoundFromCache(Integer gameRoundId) {
        String key = StadiumRedisConstants.getGameRoundKey(gameRoundId);
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        myRedisTemplate.delteKey(redisKey);
    }

    @Override
    public void initGameRoundToCache(List<GameRound> gameRounds) {
        if (null != gameRounds && gameRounds.size() > 0) {
            for (GameRound gameRound : gameRounds) {
                this.initGameRoundToCache(gameRound);
            }
        }
    }

    @Override
    public void initGameScheduleToCache(GameSchedule gameSchedule) {
        String key = StadiumRedisConstants.getGameScheduleKey(gameSchedule.getId());
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        RedisKey redisFlagKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, "GAME_SCHEDULE_UPDATE_KEY_"+gameSchedule.getId());
        long flag = myRedisTemplate.setnx(redisFlagKey);
        if (flag != 0) {
            myRedisTemplate.setWithExpireTime(redisKey, gameSchedule, EXPIRE_TIME);
            myRedisTemplate.delteKey(redisFlagKey);
        }
    }

    @Override
    public void delGameScheduleFromCache(Integer gameScheduleId) {
        String key = StadiumRedisConstants.getGameScheduleKey(gameScheduleId);
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        myRedisTemplate.delteKey(redisKey);
    }

    @Override
    public void initGameScheduleToCache(List<GameSchedule> gameSchedules) {
        if (null != gameSchedules && gameSchedules.size() > 0) {
            for (GameSchedule gameSchedule : gameSchedules) {
                this.initGameScheduleToCache(gameSchedule);
            }
        }
    }

    @Override
    public GameEvent getGameEventFromCache(Integer gameEventId) {
        String key = StadiumRedisConstants.getGameEventKey(gameEventId);
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        return (GameEvent) myRedisTemplate.get(redisKey);
    }

    @Override
    public GameRound getGameRoundFromCache(Integer gameRoundId) {
        String key = StadiumRedisConstants.getGameRoundKey(gameRoundId);
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        return (GameRound) myRedisTemplate.get(redisKey);
    }

    @Override
    public GameSchedule getGameScheduleFromCache(Integer gameScheduleId) {
        String key = StadiumRedisConstants.getGameScheduleKey(gameScheduleId);
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
        return (GameSchedule) myRedisTemplate.get(redisKey);
    }

    @Override
    public void initGameEventConfig(GameEventConfig gameEventConfig, int type) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, DEFAULT_GAME_CONFIG_KEY+type);
        RedisKey redisFlagKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, "DEFAULT_GAME_CONFIG_KEY_FLAG_"+type);
        long flag = myRedisTemplate.setnx(redisFlagKey);
        if (flag != 0) {
            myRedisTemplate.setWithExpireTime(redisKey, gameEventConfig, EXPIRE_TIME);
            myRedisTemplate.delteKey(redisFlagKey);
        }
    }

    @Override
    public void delGameEventConfig(int type) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, DEFAULT_GAME_CONFIG_KEY+type);
        myRedisTemplate.delteKey(redisKey);
    }

    @Override
    public GameEventConfig getGameEventConfig(int type) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, DEFAULT_GAME_CONFIG_KEY+type);
        return (GameEventConfig) myRedisTemplate.get(redisKey);
    }

    @Override
    public void initClubGameEventConfig(ClubGameEventConfig clubGameEventConfig) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, CLUB_GAME_CONFIG_KEY);
        RedisKey redisFlagKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, "CLUB_GAME_CONFIG_KEY_FLAG");
        long flag = myRedisTemplate.setnx(redisFlagKey);
        if (flag != 0) {
            myRedisTemplate.setWithExpireTime(redisKey, clubGameEventConfig, EXPIRE_TIME);
            myRedisTemplate.delteKey(redisFlagKey);
        }
    }

    @Override
    public ClubGameEventConfig getClubGameEventConfig() {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, CLUB_GAME_CONFIG_KEY);
        return (ClubGameEventConfig) myRedisTemplate.get(redisKey);
    }

    @Override
    public void deleteClubGameEventConfig() {
        myRedisTemplate.delteKey(new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, CLUB_GAME_CONFIG_KEY));
        myRedisTemplate.delteKey(new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, "CLUB_GAME_CONFIG_KEY_FLAG"));
    }

    @Override
    public void updateBlackUser(int uid) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, BLUCK_USER_KEY);
        myRedisTemplate.sadd(redisKey, uid);
    }

    @Override
    public List<Integer> queryBlackUser() {
        List<Integer> result = new ArrayList<>();
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, BLUCK_USER_KEY);
        Set<byte[]> temp = myRedisTemplate.smembers(redisKey);
        if (CollectionUtils.isNotEmpty(temp)) {
            try {
                for (byte[] t : temp) {
                    result.add((Integer)SerializeUtil.unserialize(t));
                }
            } catch (Exception e) {
                logger.error("解析用户黑名单缓存信息出错:{}", e);
            }
        }
        return result;
    }

    @Override
    public void deleteAllBlackUser() {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, BLUCK_USER_KEY);
        myRedisTemplate.delteKey(redisKey);
    }

    @Override
    public void deleteOneBlackUser(int uid) {
        RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_DLUB_NEW_KEY, BLUCK_USER_KEY);
        myRedisTemplate.spop(redisKey, uid);
    }
}
